package com.woniuxy.ticketmovie.controller;

import com.woniuxy.ticketmovie.checkoutGroup.TicketTypeId;
import com.woniuxy.ticketmovie.entity.ResponseResult;
import com.woniuxy.ticketmovie.entity.TicketType;
import com.woniuxy.ticketmovie.service.TypeService;
import com.woniuxy.ticketmovie.exception.NameExsitException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName TypeController
 * @Description TODO
 * @Author zx
 * @Date 2021-02-22 22:17:36
 * @Version 1.0
 */
@Validated
@Controller
@RequestMapping("type")
public class TypeController {

    @Resource
    private TypeService typeService;

    @ResponseBody
    @RequestMapping(value = "findById/{id}",method = RequestMethod.POST)
    public ResponseResult<TicketType> findById(@PathVariable(value = "id") @Min(value = 1,message = "id有误") int id){
        ResponseResult<TicketType> responseResult = new ResponseResult<>();
        responseResult.setData(typeService.getById(id));
        responseResult.setCode(200);
        responseResult.setMsg("查询成功！");
        return responseResult;
    }

    @ResponseBody
    @PostMapping("list")
    public ResponseResult<List<TicketType>> list(@RequestBody @Validated(value = TicketTypeId.class) TicketType ticketType){
        ResponseResult<List<TicketType>> responseResult = new ResponseResult<>();
        responseResult.setData(typeService.list());
        responseResult.setCode(200);
        responseResult.setMsg("查询成功！");
        return responseResult;
    }


    //BindingResult result
    @PostMapping("add")
    @ResponseBody
    public ResponseResult<Object> add(@RequestBody @Valid TicketType ticketType){
        try{
            typeService.add(ticketType);
        }catch (NameExsitException e){
            return new ResponseResult<>(101,"名称重复");
        }
        return new ResponseResult(200,"ok");
    }
}
